package slxy.jiangtao.dts.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import slxy.jiangtao.dts.core.common.bean.Constants;
import slxy.jiangtao.dts.core.common.bean.FormQuery;
import slxy.jiangtao.dts.core.common.bean.JSONResult;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.core.util.AppException;
import slxy.jiangtao.dts.entity.sys.Menu;
import slxy.jiangtao.dts.entity.sys.User;
import slxy.jiangtao.dts.service.sys.MenuSevice;
import slxy.jiangtao.dts.service.sys.UserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Code must be sexy
 * Created by JiangTao on 2017/1/11.
 */
@Controller
@RequestMapping("/")
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private CommonService commonService;
    @Autowired
    private UserService userService;
    @Autowired
    private MenuSevice menuSevice;

    /**
     * 首页
     *
     * @param modelMap
     * @param request
     * @return
     */
    @RequestMapping(value = "index",method = RequestMethod.GET)
    public String index(ModelMap modelMap,HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_IN_SESSION);
        List<Menu> menuList = menuSevice.getRoleMenus(user.getRoleId());
        modelMap.put("menuList",menuList);
        return "index";
    }

    /**首页加载的内容页面*/
    @RequestMapping("first")
    public String first(ModelMap modelMap){
        String newssql = "select id,title,file_no from news order by id desc limit 15";
        List<Map> newslist = commonService.findListMapBySql(newssql);
        String noticesql = "select id,name,file_no from notice order by id desc limit 10";
        List<Map> noticelist = commonService.findListMapBySql(noticesql);
        String articlesql = "select id,name,file_no from article order by id desc limit 10";
        List<Map> articlelist = commonService.findListMapBySql(articlesql);
        modelMap.put("newsList",newslist);
        modelMap.put("noticeList",noticelist);
        modelMap.put("articlelist",articlelist);
        return "first";
    }


    /**
     * 通用form表单查询
     */

    @RequestMapping(value = "query", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult queryData(@RequestBody FormQuery formQuery) {
        return JSONResult.success(commonService.getPage(formQuery));
    }

    /**
     * 登录页面
     */
    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String loginGet() {
        return "admin/login";
    }

    /**
     * 登录提交
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String loginPost(HttpServletRequest request, String userno, String passwordhash, ModelMap modelMap, HttpServletResponse response) throws Exception {
        try {
            boolean loginCheck = userService.loginCheck(request, userno.trim(), passwordhash);
            if (!loginCheck) {
                modelMap.addAttribute("message", "账户或密码错误");
                return "admin/login";
            }
            request.getSession().setMaxInactiveInterval(3600);
            Cookie cookie = new Cookie("syscookiename", userno);
            cookie.setMaxAge(-1);
            cookie.setPath("/");
            response.addCookie(cookie);
            return "redirect:index.shtml";
        } catch (AppException e) {
            modelMap.addAttribute("message", (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
            return "admin/login";
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.addAttribute("message", "系统异常，登录失败！" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
            return "admin/login";
        }
    }

    /**
     * 退出系统
     */
    @RequestMapping("loginOut")
    public String loginOut(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "admin/login";
    }



}
